﻿/*
department : beacon
write by   : wuweiwei
github     : www.github.com/flybirdsoft
date       : 2015/10/28
*/

(function(win){
	var wMath = {
		version : "1.0"
	};

	wMath.round = function(v,cnt){
		var r=0;
		if(cnt==undefined || cnt ==0)
		{
			return parseInt(v);
		}
		if(cnt<0)
		{
			return v;
		}
		r = parseInt(v * Math.pow(10,cnt)+0.5) / Math.pow(10,cnt);
		return r;
	};

	wMath.currency = function(v){
		var i , vStr = v.toString(), len = vStr.length;
		var r = "" , n = 0 , end = "" ,pos=0;
		if(parseInt(v)<1000)
		{
			return;
		}
		if(vStr.indexOf(".")==-1)
		{
			for(i=len-1 ; i>=0 ; i--)
			{
				r = vStr.substr(i,1) + r;
				n++;
				if(n==3)
				{
					r = "," + r;
					n=0;
				}
			}
			if(r.substr(0,1)==",")
			{
				return r.substr(1);
			}
			return r;
		}
		else
		{
			pos = vStr.indexOf(".");
			end = vStr.substr(pos);
			vStr = vStr.substr(0,pos);
			len = vStr.length;
			for(i=len-1 ; i>=0 ; i--)
			{
				r = vStr.substr(i,1) + r;
				n++;
				if(n==3)
				{
					r = "," + r;
					n=0;
				}
			}
			if(r.substr(0,1)==",")
			{
				return r.substr(1);
			}
			return r+end;	
		}
	};

	wMath.convertToDate = function(millionSeconds,formatStr,language){
		/*
		formatStr = "yyyy/mm/dd hh:mm:ss w"
		language = "en|cn"
		*/
		var str = "" , week="";
		var y,m,d,h,mm,s,w;
		var _formatStr ;

		if(millionSeconds == undefined || typeof(millionSeconds)!="number")
		{
			return "error";
		}

		var date = new Date(millionSeconds);
		if(formatStr!=undefined)
		{
			_formatStr = formatStr.toUpperCase();
		}
		y = date.getFullYear();
		m = (date.getMonth()+1);
		d = date.getDate();
		w = date.getDay();
		h = date.getHours();
		mm = date.getMinutes();
		s = date.getSeconds();

		var weekdayEn=new Array(7);
		weekdayEn[0]="Sunday";
		weekdayEn[1]="Monday";
		weekdayEn[2]="Tuesday";
		weekdayEn[3]="Wednesday";
		weekdayEn[4]="Thursday";
		weekdayEn[5]="Friday";
		weekdayEn[6]="Saturday";

		var weekdayCn=new Array(7);
		weekdayCn[0]="日";
		weekdayCn[1]="一";
		weekdayCn[2]="二";
		weekdayCn[3]="三";
		weekdayCn[4]="四";
		weekdayCn[5]="五";
		weekdayCn[6]="六";

		var weekdayN=new Array(7);
		weekdayN[0]="7";
		weekdayN[1]="1";
		weekdayN[2]="2";
		weekdayN[3]="3";
		weekdayN[4]="4";
		weekdayN[5]="5";
		weekdayN[6]="6";		

		if(formatStr==undefined)
		{
			str = y + "/" + m + "/" +d + " " + h + ":" + mm + ":" + s;
		}
		else if(formatStr!=undefined && typeof(formatStr)=="string")
		{
			str = _formatStr.replace("YYYY",y);
			str = str.replace("MM" , m<10 ? "0"+m : m);
			str = str.replace("DD" , d<10 ? "0"+d : d);
			str = str.replace("HH" , h<10 ? "0"+h : h);
			str = str.replace("MM" , mm<10 ? "0"+mm : mm);
			str = str.replace("SS" , s<10 ? "0"+s : s);

			str = str.replace("M" , m);
			str = str.replace("D" , d);
			str = str.replace("H" , h);
			str = str.replace("M" , mm);
			str = str.replace("S" , s);

			if(language==undefined)
			{
				str = str.replace("W" , weekdayN[w]);
			}
			else if(language.toUpperCase() == "CN")
			{
				str = str.replace("W" , weekdayCn[w]);
			}
			else if(language.toUpperCase()  == "EN")
			{
				str = str.replace("W" , weekdayEn[w]);
			}
		}

		return str;
	};

	win["wMath"] = wMath;
})(window);

